﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.IO;
using ImageTools;
using ImageTools.IO.Png;

namespace AdventureWorksLT.Controls
{
    public partial class WebcamWindow : ChildWindow
    {
        public WebcamWindow()
        {
            InitializeComponent();
            StartWebcam();
        }

        private CaptureSource cs = null;


        private void StartWebcam()
        {
            if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
            {
                VideoCaptureDevice vcd = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

                if (null != vcd)
                {
                    cs = new CaptureSource();
                    cs.VideoCaptureDevice = vcd;
                    cs.Start();
                    VideoBrush videoBrush = new VideoBrush();
                    videoBrush.Stretch = Stretch.UniformToFill;
                    videoBrush.SetSource(cs);
                    webcamRectangle.Fill = videoBrush;
                    cs.CaptureImageCompleted += cs_CaptureImageCompleted;
                }
                else
                {
                    MessageBox.Show("Error initializing Webcam or Mic.\nPlease install device drivers and try again");
                }
            }
        }

        private void StopWebcam()
        {
            if (null != cs)
            {
                cs.Stop();
            }
        }




        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            cs.Stop();
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            cs.Stop();
            this.DialogResult = false;
        }

        private void photoButton_Click(object sender, RoutedEventArgs e)
        {
            if (null != cs)
            {
                if (cs.State == CaptureState.Started)
                {
                    cs.CaptureImageAsync();
                }
                else
                    MessageBox.Show("Start capture from Webcam and try again!");
            }
            else
                MessageBox.Show("Start capture from Webcam and try again!");
        }


        void cs_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
        {
            var bitmap = e.Result;
            var img = bitmap.ToImage();
            byte[] ba;

            var encoder = new PngEncoder();
            MemoryStream stream = new MemoryStream();
            encoder.Encode(img, stream);


            ba = stream.ToArray();
            stream.Close();

            picturesListBox.Items.Insert(0, ba);
            picturesListBox.SelectedItem = ba;
            picturesListBox.ScrollIntoView(ba);
            
        }

        private void picturesListBox_Drop(object sender, DragEventArgs e)
        {
            IDataObject data = e.Data;

            FileInfo[] files = (FileInfo[])data.GetData(DataFormats.FileDrop);

            foreach (FileInfo f in files)
            {
                if (f.Extension == ".png" || f.Extension == ".jpg")
                {
                    byte[] b;
                    FileStream s = f.OpenRead();
                    MemoryStream ms = new MemoryStream();
                    s.CopyTo(ms);
                    b = ms.ToArray();
                    ms.Close();
                    s.Close();

                    picturesListBox.Items.Insert(0, b);
                    picturesListBox.SelectedItem = b;
                    picturesListBox.ScrollIntoView(b);
                }
            }


        }


    }
}

